%{

    #include <string.h>

    #include "win32define.h"

    #include "instructions.h"
    #include "y.tab.h"

    int nbLines = 0 ;

%}

%option nounput
%option noinput

REG_VARNAME [a-zA-Z][a-zA-Z0-9_]*

REG_STRING "\""(\\.|[^"])*"\""

REG_COMMENT "//"(.*)$

REG_NUMBER [0-9]+
REG_HEXA 0x[a-zA-Z0-9]+
REG_INTEGER {REG_NUMBER}|{REG_HEXA}

%%

"__asm__" {return tASM ; }

"{" {return tAC_OPEN;}
"}" {return tAC_CLOSE;}

"(" {return tPA_OPEN;}
")" {return tPA_CLOSE;}

"&&" {return tOP_AND;}
"||" {return tOP_OR;}	

"?" {return tOP_TER;}
":" {return tCOLON;}

"+=" {return tOP_AFPLUS;}
"-=" {return tOP_AFLESS;}
"*=" {return tOP_AFMUL;}
"/=" {return tOP_AFDIV;}
">>=" {return tOP_AFRSHIFT;}
"<<=" {return tOP_AFLSHIFT;}
"&=" {return tOP_AFAND;}
"|=" {return tOP_AFOR;}
"^=" {return tOP_AFXOR;}
"~=" {return tOP_AFNOT;}

"&" {return tOP_BAND;}
"|" {return tOP_BOR;}
"^" {return tOP_BXOR;}
"~" {return tOP_BNOT;}

"<<" {return tOP_LSHIFT;}
">>" {return tOP_RSHIFT;}

"==" {return tOP_EQ;}
"!=" {return tOP_NE;}
"<"  {return tOP_LT;}
"<=" {return tOP_LE;}
">"  {return tOP_GT;}
">=" {return tOP_GE;}
"++" {return tOP_PPLUS;}
"--" {return tOP_LLESS;}

"!" {return tOP_NOT;}

"+" {return tOP_PLUS;}
"-" {return tOP_LESS;}
"*" {return tOP_MUL;}
"/" {return tOP_DIV;}
"=" {return tOP_AFC;}

{REG_COMMENT} {
    yylval.comment = strdup(yytext+2) ;
    return tCOMMENT_LINE ;
}

{REG_STRING} {
    yylval.str = strdup(yytext+1) ;
    yylval.str[yyleng-2] = '\0' ;
    return tSTRING ;
}

"int" {return tINT;}
"const" {return tCONST;}
"void" {return tVOID;}
"return" {return tRETURN;}

","   {return tCOMA;}
";"   {return tEND;}

"if"  {return tIF;}
"else" {return tELSE;}
"while" {return tWHILE;}
"for" {return tFOR;}
"break" {return tBREAK;}
"continue" {return tCONTINUE;}

{REG_VARNAME} {
    if (yyleng >= 64) {
	return -1 ;
    }
    yylval.id = strdup(yytext) ;
    return tVARIABLE;
}

{REG_INTEGER} {
    yylval.val = strtol(yytext, NULL, yytext[1] == 'x' ? 16 : 10) ;
    return tINTEGER;
}

[\t ] {};

\n    {nbLines++;}

.     {
    return -1 ;
};

%%

#ifdef __LEX_MAIN

int main () {
    return yylex() ;
}

#endif
